/**
 * @author hugh
 * @time  2021-01-10 11:40
 * @title  http请求封装
 * @desc
 *
 */
import { Interceptors } from './interceptors';
import { ElMessage } from 'element-plus';
import router from '../router';

export class HttpService {
  public axios: any;
  public modal: any;
  static httpService:any=null;
  constructor() {
      // 获取axios实例
    this.axios = new Interceptors().getInterceptors();
  }

	static  getInterceptors(){
		if(this.httpService==null){
			this.httpService=new HttpService();
		}
		return this.httpService;
	}

	public getUrl(url:string){
		if(url.indexOf('http')!==-1){
		  return url;
		}
		return process.env.VUE_APP_BASE_URL+url;
	}

 /**
   * get请求
   * @param params 参数
   * @param jwt  是否token校验
   * @param url  接口
   */
  public getData(url: string,params:any,isJwt:boolean) {
    const headers={
      isJwt: isJwt
    }
     let reUrl = this.getUrl(url);
    return new Promise((resolve, reject) => {
      this.axios.get(reUrl, {
        params: params,
        headers: headers,
      }).then((res:any) => {
        this.resultHandle(res, resolve);
      }).catch((err:any) => {
        reject(err.message);
      });
    });

  }

  /**
   * post请求
   * @param params 参数
   * @param jwt  是否token校验
   * @param url   接口
   */
  public postParams(url: string,params: any,isJwt:boolean) {
    const headers={
    	isJwt: isJwt
    }
    let reUrl = this.getUrl(url);
    return new Promise((resolve, reject) => {
      this.axios.post(reUrl, params, {
        headers: headers,
      }).then((res:any) => {
        this.resultHandle(res, resolve);
      }).catch((err:any) => {
        reject(err.message);
      });
    });
  }
  /**
   * post fromdata请求
   * @param params 参数
   * @param jwt  是否token校验
   * @param url   接口
   */
  public postFromData(url: string,data: any,headers:any) {
    let reUrl = this.getUrl(url);
    let formData = new FormData();
    for(var key in data){
      formData.append(key,data[key]);
    }
    return new Promise((resolve, reject) => {
      this.axios({
        url:reUrl,
        method:'post',
        headers: headers,
        data:formData
      }).then((res:any) => {
        this.resultHandle(res, resolve);
      }).catch((err:any) => {
        reject(err.message);
      });
    });
  }
  /**
   * post mock请求
   * @param params 参数
   * @param jwt  是否token校验
   * @param url   接口
   */
  public postMockData(url: string,params: any) {
    return new Promise((resolve, reject) => {
      this.axios.post(url, params, {
        headers: {
          isJwt: false
        },
        }).then((res:any) => {
          this.resultHandle(res, resolve);
        }).catch((err:any) => {
          reject(err.message);
        });
    });
  }
  /**
   * get请求-mock
   * @param params 参数
   * @param jwt  是否token校验
   * @param url  接口
   */
  public getDataMock(url: string,params:any) {

    return new Promise((resolve, reject) => {
      this.axios.get(url, {
        params: params,
        headers: {
          isJwt: false
        }
      }).then((res:any) => {
        console.info('any',res);
        this.resultHandle(res, resolve);
      }).catch((err:any) => {
        console.info('any',err);
        reject(err.message);
      });
    });

  }
  /**
   *
   * @param res
   * @param resolve
   */
  public resultHandle(res: any, resolve:any) {
    if (res.code===0) {
      if(!res.data){
         resolve([]);
      }else{
         resolve(res.data);
      }

    } else {
      this.errorHandle(res);
    }
  }


  /**
   * 服务端状态处理,例如中断性异常,退出异常等等(与拦截器http握手状态注意区分,一般都能分清楚吧)
   * @param res
   */
  public errorHandle(res: any) {
     // 统一谈服务端提示,我们提示统一由服务端提供
    ElMessage.warning({
               message: res.msg,
               type: 'warning'
             });

    // 状态码判断
    switch (res.code) {
      case -1:

        break;
      case 50010://凭证过期
       router.push('Login');
        break;
      case 50011://凭证无效或已过期
       router.push('Login');
        break;
      case 50012://刷新凭证无效或已过期
       router.push('Login');
        break;
      case 50013://拒绝访问
        break;
      case 50014://无权限访问
        break;
      case 50015://请求无效或则请求不接受
         break;
      case 50016://范围无效
         break;
      case 50017://客户无效
         break;
      default:
      // console.log(other);
    }
  }

}
